<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>3. Kubernetes详细教程-资源管理 | 小鹏杂谈</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="小鹏杂谈">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.8516fda1.css" as="style"><link rel="preload" href="/assets/js/app.b9ccfc55.js" as="script"><link rel="preload" href="/assets/js/4.8390fbcb.js" as="script"><link rel="preload" href="/assets/js/1.3334f995.js" as="script"><link rel="preload" href="/assets/js/31.fa9bd0f8.js" as="script"><link rel="prefetch" href="/assets/js/10.8eb4e356.js"><link rel="prefetch" href="/assets/js/11.31befa4c.js"><link rel="prefetch" href="/assets/js/12.da37eb75.js"><link rel="prefetch" href="/assets/js/13.b24ba73c.js"><link rel="prefetch" href="/assets/js/14.3bbed283.js"><link rel="prefetch" href="/assets/js/15.fac60f8d.js"><link rel="prefetch" href="/assets/js/16.b75d2036.js"><link rel="prefetch" href="/assets/js/17.116317c5.js"><link rel="prefetch" href="/assets/js/18.80fdbeda.js"><link rel="prefetch" href="/assets/js/19.459fc46f.js"><link rel="prefetch" href="/assets/js/20.452f30f9.js"><link rel="prefetch" href="/assets/js/21.99f641dc.js"><link rel="prefetch" href="/assets/js/22.081dc33b.js"><link rel="prefetch" href="/assets/js/23.d3455a59.js"><link rel="prefetch" href="/assets/js/24.8f5d39e0.js"><link rel="prefetch" href="/assets/js/25.c3dc47fb.js"><link rel="prefetch" href="/assets/js/26.d93b5cf2.js"><link rel="prefetch" href="/assets/js/27.6d2089a5.js"><link rel="prefetch" href="/assets/js/28.57336847.js"><link rel="prefetch" href="/assets/js/29.903b5e06.js"><link rel="prefetch" href="/assets/js/3.32100170.js"><link rel="prefetch" href="/assets/js/30.274666a3.js"><link rel="prefetch" href="/assets/js/32.20179720.js"><link rel="prefetch" href="/assets/js/33.2dcc611d.js"><link rel="prefetch" href="/assets/js/34.ba79122c.js"><link rel="prefetch" href="/assets/js/35.bcd9298f.js"><link rel="prefetch" href="/assets/js/36.bf6245d9.js"><link rel="prefetch" href="/assets/js/37.734eb893.js"><link rel="prefetch" href="/assets/js/38.26999070.js"><link rel="prefetch" href="/assets/js/39.cc1f016d.js"><link rel="prefetch" href="/assets/js/40.da84c263.js"><link rel="prefetch" href="/assets/js/41.a96b8efa.js"><link rel="prefetch" href="/assets/js/42.d586cccb.js"><link rel="prefetch" href="/assets/js/43.69bb61aa.js"><link rel="prefetch" href="/assets/js/44.3be5a840.js"><link rel="prefetch" href="/assets/js/45.208a3bc3.js"><link rel="prefetch" href="/assets/js/46.2d6cacad.js"><link rel="prefetch" href="/assets/js/47.5add378a.js"><link rel="prefetch" href="/assets/js/48.587139cc.js"><link rel="prefetch" href="/assets/js/49.57f7c25a.js"><link rel="prefetch" href="/assets/js/5.ff1e84d1.js"><link rel="prefetch" href="/assets/js/50.efde6e4a.js"><link rel="prefetch" href="/assets/js/51.c9447e5f.js"><link rel="prefetch" href="/assets/js/52.57a51c35.js"><link rel="prefetch" href="/assets/js/53.4be0f240.js"><link rel="prefetch" href="/assets/js/54.8dabd349.js"><link rel="prefetch" href="/assets/js/55.3b8ae0e4.js"><link rel="prefetch" href="/assets/js/56.07e830b2.js"><link rel="prefetch" href="/assets/js/57.8c0dbdcc.js"><link rel="prefetch" href="/assets/js/58.63387df6.js"><link rel="prefetch" href="/assets/js/59.959bb8c8.js"><link rel="prefetch" href="/assets/js/6.56a2b5cd.js"><link rel="prefetch" href="/assets/js/60.1041d86f.js"><link rel="prefetch" href="/assets/js/61.f58e6c18.js"><link rel="prefetch" href="/assets/js/62.a1a460c7.js"><link rel="prefetch" href="/assets/js/63.6c611c7d.js"><link rel="prefetch" href="/assets/js/64.6711119c.js"><link rel="prefetch" href="/assets/js/65.4655c3b1.js"><link rel="prefetch" href="/assets/js/66.e5dfe864.js"><link rel="prefetch" href="/assets/js/67.37515de0.js"><link rel="prefetch" href="/assets/js/68.0c8ba92d.js"><link rel="prefetch" href="/assets/js/69.52121d38.js"><link rel="prefetch" href="/assets/js/7.6f143d9f.js"><link rel="prefetch" href="/assets/js/70.1ab4d6a1.js"><link rel="prefetch" href="/assets/js/71.dc99dd1d.js"><link rel="prefetch" href="/assets/js/72.02e21e52.js"><link rel="prefetch" href="/assets/js/73.21efc121.js"><link rel="prefetch" href="/assets/js/74.649f4e1d.js"><link rel="prefetch" href="/assets/js/75.331c13e7.js"><link rel="prefetch" href="/assets/js/76.a94b5ff0.js"><link rel="prefetch" href="/assets/js/8.d4d40f2a.js"><link rel="prefetch" href="/assets/js/9.1b8824e7.js">
    <link rel="stylesheet" href="/assets/css/0.styles.8516fda1.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>小鹏杂谈</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小鹏杂谈" class="logo"> <span class="site-name">小鹏杂谈</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    lzpeng723
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>66</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>19</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/运维/" class="nav-link"><i class="undefined"></i>
  运维
</a></li><li class="dropdown-item"><!----> <a href="/categories/开发/" class="nav-link"><i class="undefined"></i>
  开发
</a></li><li class="dropdown-item"><!----> <a href="/categories/后端/" class="nav-link"><i class="undefined"></i>
  后端
</a></li><li class="dropdown-item"><!----> <a href="/categories/前端/" class="nav-link"><i class="undefined"></i>
  前端
</a></li><li class="dropdown-item"><!----> <a href="/categories/软件/" class="nav-link"><i class="undefined"></i>
  软件
</a></li><li class="dropdown-item"><!----> <a href="/categories/文档/" class="nav-link"><i class="undefined"></i>
  文档
</a></li><li class="dropdown-item"><!----> <a href="/categories/数据库/" class="nav-link"><i class="undefined"></i>
  数据库
</a></li><li class="dropdown-item"><!----> <a href="/categories/Node.js/" class="nav-link"><i class="undefined"></i>
  Node.js
</a></li><li class="dropdown-item"><!----> <a href="/categories/游戏/" class="nav-link"><i class="undefined"></i>
  游戏
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  时间线
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      文档
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/docs/minimal-boot/" class="nav-link"><i class="undefined"></i>
  minimal-boot
</a></li><li class="dropdown-item"><!----> <a href="/docs/minimal-cloud/" class="nav-link"><i class="undefined"></i>
  minimal-cloud
</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-message"></i>
      联系方式
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-github"></i>
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://gitee.com/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-mayun"></i>
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li><li class="dropdown-item"><!----> <a href="https://www.zhihu.com/people/lzpeng723" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-zhihu"></i>
  知乎
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav> <!----> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>3. Kubernetes详细教程-资源管理</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>lzpeng723</span>
            
          <span data-v-4e82dffc>2021 - </span>
          2022
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">3. Kubernetes详细教程-资源管理</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>lzpeng723</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>10/30/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>Kubernetes</span></i></div></div> <div class="theme-reco-content content__default"><p><a href="https://gitee.com/yooome/golang/blob/main/k8s%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B/Kubernetes%E8%AF%A6%E7%BB%86%E6%95%99%E7%A8%8B.md#3-%E8%B5%84%E6%BA%90%E7%AE%A1%E7%90%86" target="_blank" rel="noopener noreferrer">原文链接<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a> <a href="https://www.bilibili.com/video/BV1Qv41167ck?p=12" target="_blank" rel="noopener noreferrer">视频教程<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <h1 id="_4-资源管理"><a href="#_4-资源管理" class="header-anchor">#</a> 4. 资源管理</h1> <h2 id="_3-1-资源管理介绍"><a href="#_3-1-资源管理介绍" class="header-anchor">#</a> 3.1 资源管理介绍</h2> <p>在kubernetes中，所有的内容都抽象为资源，用户需要通过操作资源来管理kubernetes。</p> <blockquote><p>kubernetes的本质上就是一个集群系统，用户可以在集群中部署各种服务，所谓的部署服务，其实就是在kubernetes集群中运行一个个的容器，并将指定的程序跑在容器中。</p> <p>kubernetes的最小管理单元是pod而不是容器，所以只能将容器放在<code>Pod</code>中，而kubernetes一般也不会直接管理Pod，而是通过<code>Pod控制器</code>来管理Pod的。</p> <p>Pod可以提供服务之后，就要考虑如何访问Pod中服务，kubernetes提供了<code>Service</code>资源实现这个功能。</p> <p>当然，如果Pod中程序的数据需要持久化，kubernetes还提供了各种<code>存储</code>系统。</p></blockquote> <p><img src="/assets/img/image-20200406225334627.102ffc00.png" alt="image-20200406225334627"></p> <blockquote><p>学习kubernetes的核心，就是学习如何对集群上的<code>Pod、Pod控制器、Service、存储</code>等各种资源进行操作</p></blockquote> <h2 id="_3-2-yaml语言介绍"><a href="#_3-2-yaml语言介绍" class="header-anchor">#</a> 3.2 YAML语言介绍</h2> <p>YAML是一个类似 XML、JSON 的标记性语言。它强调以<strong>数据</strong>为中心，并不是以标识语言为重点。因而YAML本身的定义比较简单，号称&quot;一种人性化的数据格式语言&quot;。</p> <div class="language-xml line-numbers-mode"><pre class="language-xml"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>heima</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>age</span><span class="token punctuation">&gt;</span></span>15<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>age</span><span class="token punctuation">&gt;</span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>address</span><span class="token punctuation">&gt;</span></span>Beijing<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>address</span><span class="token punctuation">&gt;</span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>heima</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token punctuation">---</span>
<span class="token key atrule">heima</span><span class="token punctuation">:</span>
  <span class="token key atrule">age</span><span class="token punctuation">:</span> <span class="token number">15</span>
  <span class="token key atrule">address</span><span class="token punctuation">:</span> Beijing
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>YAML的语法比较简单，主要有下面几个：</p> <ul><li>大小写敏感</li> <li>使用缩进表示层级关系</li> <li>缩进不允许使用tab，只允许空格( 低版本限制 )</li> <li>缩进的空格数不重要，只要相同层级的元素左对齐即可</li> <li>'#'表示注释</li></ul> <p>YAML支持以下几种数据类型：</p> <ul><li>纯量：单个的、不可再分的值</li> <li>对象：键值对的集合，又称为映射（mapping）/ 哈希（hash） / 字典（dictionary）</li> <li>数组：一组按次序排列的值，又称为序列（sequence） / 列表（list）</li></ul> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token comment"># 纯量, 就是指的一个简单的值，字符串、布尔值、整数、浮点数、Null、时间、日期</span>
<span class="token comment"># 1 布尔类型</span>
<span class="token key atrule">c1</span><span class="token punctuation">:</span> true (或者True)
<span class="token comment"># 2 整型</span>
<span class="token key atrule">c2</span><span class="token punctuation">:</span> <span class="token number">234</span>
<span class="token comment"># 3 浮点型</span>
<span class="token key atrule">c3</span><span class="token punctuation">:</span> <span class="token number">3.14</span>
<span class="token comment"># 4 null类型 </span>
<span class="token key atrule">c4</span><span class="token punctuation">:</span> <span class="token null important">~</span>  <span class="token comment"># 使用~表示null</span>
<span class="token comment"># 5 日期类型</span>
<span class="token key atrule">c5</span><span class="token punctuation">:</span> <span class="token datetime number">2018-02-17</span>    <span class="token comment"># 日期必须使用ISO 8601格式，即yyyy-MM-dd</span>
<span class="token comment"># 6 时间类型</span>
<span class="token key atrule">c6</span><span class="token punctuation">:</span> <span class="token datetime number">2018-02-17T15:02:31+08:00</span>  <span class="token comment"># 时间使用ISO 8601格式，时间和日期之间使用T连接，最后使用+代表时区</span>
<span class="token comment"># 7 字符串类型</span>
<span class="token key atrule">c7</span><span class="token punctuation">:</span> heima     <span class="token comment"># 简单写法，直接写值 , 如果字符串中间有特殊字符，必须使用双引号或者单引号包裹 </span>
<span class="token key atrule">c8</span><span class="token punctuation">:</span> line1
    line2     <span class="token comment"># 字符串过多的情况可以拆成多行，每一行会被转化成一个空格</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token punctuation">---</span>
<span class="token comment"># 对象</span>
<span class="token comment"># 形式一(推荐):</span>
<span class="token key atrule">heima</span><span class="token punctuation">:</span>
  <span class="token key atrule">age</span><span class="token punctuation">:</span> <span class="token number">15</span>
  <span class="token key atrule">address</span><span class="token punctuation">:</span> Beijing
<span class="token punctuation">---</span>
<span class="token comment"># 形式二(了解):</span>
<span class="token key atrule">heima</span><span class="token punctuation">:</span> <span class="token punctuation">{</span><span class="token key atrule">age</span><span class="token punctuation">:</span> <span class="token number">15</span><span class="token punctuation">,</span><span class="token key atrule">address</span><span class="token punctuation">:</span> Beijing<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token comment"># 数组</span>
<span class="token punctuation">---</span>
<span class="token comment"># 形式一(推荐):</span>
<span class="token key atrule">address</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> 顺义
  <span class="token punctuation">-</span> 昌平
<span class="token punctuation">---</span>
<span class="token comment"># 形式二(了解):</span>
<span class="token key atrule">address</span><span class="token punctuation">:</span> <span class="token punctuation">[</span>顺义<span class="token punctuation">,</span>昌平<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><blockquote><p>小提示：</p> <p>1 书写yaml切记<code>:</code> 后面要加一个空格</p> <p>2 如果需要将多段yaml配置放在一个文件中，中间要使用<code>---</code>分隔</p> <p>3 下面是一个yaml转json的网站，可以通过它验证yaml是否书写正确</p> <p>https://www.json2yaml.com/convert-yaml-to-json</p></blockquote> <h2 id="_3-3-资源管理方式"><a href="#_3-3-资源管理方式" class="header-anchor">#</a> 3.3 资源管理方式</h2> <ul><li><p>命令式对象管理：直接使用命令去操作kubernetes资源</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl run nginx-pod --image<span class="token operator">=</span>nginx:1.17.1 --port<span class="token operator">=</span><span class="token number">80</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>命令式对象配置：通过命令配置和配置文件去操作kubernetes资源</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl create/patch -f nginx-pod.yaml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li> <li><p>声明式对象配置：通过apply命令和配置文件去操作kubernetes资源, apply 等于 创建或更新</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl apply -f nginx-pod.yaml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div></li></ul> <table><thead><tr><th style="text-align:left;">类型</th> <th style="text-align:left;">操作对象</th> <th style="text-align:left;">适用环境</th> <th style="text-align:left;">优点</th> <th style="text-align:left;">缺点</th></tr></thead> <tbody><tr><td style="text-align:left;">命令式对象管理</td> <td style="text-align:left;">对象</td> <td style="text-align:left;">测试</td> <td style="text-align:left;">简单</td> <td style="text-align:left;">只能操作活动对象，无法审计、跟踪</td></tr> <tr><td style="text-align:left;">命令式对象配置</td> <td style="text-align:left;">文件</td> <td style="text-align:left;">开发</td> <td style="text-align:left;">可以审计、跟踪</td> <td style="text-align:left;">项目大时，配置文件多，操作麻烦</td></tr> <tr><td style="text-align:left;">声明式对象配置</td> <td style="text-align:left;">目录</td> <td style="text-align:left;">开发</td> <td style="text-align:left;">支持目录操作</td> <td style="text-align:left;">意外情况下难以调试</td></tr></tbody></table> <h3 id="_3-3-1-命令式对象管理"><a href="#_3-3-1-命令式对象管理" class="header-anchor">#</a> 3.3.1 命令式对象管理</h3> <p><strong>kubectl命令</strong></p> <p>kubectl是kubernetes集群的命令行工具，通过它能够对集群本身进行管理，并能够在集群上进行容器化应用的安装部署。kubectl命令的语法如下：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl <span class="token punctuation">[</span>command<span class="token punctuation">]</span> <span class="token punctuation">[</span>type<span class="token punctuation">]</span> <span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token punctuation">[</span>flags<span class="token punctuation">]</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><strong>comand</strong>：指定要对资源执行的操作，例如create、get、delete</p> <p><strong>type</strong>：指定资源类型，比如deployment、pod、service</p> <p><strong>name</strong>：指定资源的名称，名称大小写敏感</p> <p><strong>flags</strong>：指定额外的可选参数</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 查看所有pod</span>
kubectl get pod 

<span class="token comment"># 查看某个pod</span>
kubectl get pod pod_name

kubectl get pod pod_name -o wide
kubectl get pod pod_name -o json
kubectl get pod pod_name -o yaml
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>资源类型</strong></p> <p>kubernetes中所有的内容都抽象为资源，可以通过下面的命令进行查看:</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl api-resources
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>经常使用的资源有下面这些：</p> <table><thead><tr><th style="text-align:left;">资源分类</th> <th style="text-align:left;">资源名称</th> <th style="text-align:left;">缩写</th> <th style="text-align:left;">资源作用</th></tr></thead> <tbody><tr><td style="text-align:left;">集群级别资源</td> <td style="text-align:left;">nodes</td> <td style="text-align:left;">no</td> <td style="text-align:left;">集群组成部分</td></tr> <tr><td style="text-align:left;">namespaces</td> <td style="text-align:left;">ns</td> <td style="text-align:left;">隔离Pod</td> <td style="text-align:left;"></td></tr> <tr><td style="text-align:left;">pod资源</td> <td style="text-align:left;">pods</td> <td style="text-align:left;">po</td> <td style="text-align:left;">装载容器</td></tr> <tr><td style="text-align:left;">pod资源控制器</td> <td style="text-align:left;">replicationcontrollers</td> <td style="text-align:left;">rc</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">replicasets</td> <td style="text-align:left;">rs</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">deployments</td> <td style="text-align:left;">deploy</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">daemonsets</td> <td style="text-align:left;">ds</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">jobs</td> <td style="text-align:left;"></td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">cronjobs</td> <td style="text-align:left;">cj</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">horizontalpodautoscalers</td> <td style="text-align:left;">hpa</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">statefulsets</td> <td style="text-align:left;">sts</td> <td style="text-align:left;">控制pod资源</td></tr> <tr><td style="text-align:left;">服务发现资源</td> <td style="text-align:left;">services</td> <td style="text-align:left;">svc</td> <td style="text-align:left;">统一pod对外接口</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">ingress</td> <td style="text-align:left;">ing</td> <td style="text-align:left;">统一pod对外接口</td></tr> <tr><td style="text-align:left;">存储资源</td> <td style="text-align:left;">volumeattachments</td> <td style="text-align:left;"></td> <td style="text-align:left;">存储</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">persistentvolumes</td> <td style="text-align:left;">pv</td> <td style="text-align:left;">存储</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">persistentvolumeclaims</td> <td style="text-align:left;">pvc</td> <td style="text-align:left;">存储</td></tr> <tr><td style="text-align:left;">配置资源</td> <td style="text-align:left;">configmaps</td> <td style="text-align:left;">cm</td> <td style="text-align:left;">配置</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">secrets</td> <td style="text-align:left;"></td> <td style="text-align:left;">配置</td></tr></tbody></table> <p><strong>操作</strong></p> <p>kubernetes允许对资源进行多种操作，可以通过--help查看详细的操作命令</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl --help
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>经常使用的操作有下面这些：</p> <table><thead><tr><th style="text-align:left;">命令分类</th> <th style="text-align:left;">命令</th> <th style="text-align:left;">翻译</th> <th style="text-align:left;">命令作用</th></tr></thead> <tbody><tr><td style="text-align:left;">基本命令</td> <td style="text-align:left;">create</td> <td style="text-align:left;">创建</td> <td style="text-align:left;">创建一个资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">edit</td> <td style="text-align:left;">编辑</td> <td style="text-align:left;">编辑一个资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">get</td> <td style="text-align:left;">获取</td> <td style="text-align:left;">获取一个资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">patch</td> <td style="text-align:left;">更新</td> <td style="text-align:left;">更新一个资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">delete</td> <td style="text-align:left;">删除</td> <td style="text-align:left;">删除一个资源</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">explain</td> <td style="text-align:left;">解释</td> <td style="text-align:left;">展示资源文档</td></tr> <tr><td style="text-align:left;">运行和调试</td> <td style="text-align:left;">run</td> <td style="text-align:left;">运行</td> <td style="text-align:left;">在集群中运行一个指定的镜像</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">expose</td> <td style="text-align:left;">暴露</td> <td style="text-align:left;">暴露资源为Service</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">describe</td> <td style="text-align:left;">描述</td> <td style="text-align:left;">显示资源内部信息</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">logs</td> <td style="text-align:left;">日志输出容器在 pod 中的日志</td> <td style="text-align:left;">输出容器在 pod 中的日志</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">attach</td> <td style="text-align:left;">缠绕进入运行中的容器</td> <td style="text-align:left;">进入运行中的容器</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">exec</td> <td style="text-align:left;">执行容器中的一个命令</td> <td style="text-align:left;">执行容器中的一个命令</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">cp</td> <td style="text-align:left;">复制</td> <td style="text-align:left;">在Pod内外复制文件</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">rollout</td> <td style="text-align:left;">首次展示</td> <td style="text-align:left;">管理资源的发布</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">scale</td> <td style="text-align:left;">规模</td> <td style="text-align:left;">扩(缩)容Pod的数量</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">autoscale</td> <td style="text-align:left;">自动调整</td> <td style="text-align:left;">自动调整Pod的数量</td></tr> <tr><td style="text-align:left;">高级命令</td> <td style="text-align:left;">apply</td> <td style="text-align:left;">rc</td> <td style="text-align:left;">通过文件对资源进行配置</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">label</td> <td style="text-align:left;">标签</td> <td style="text-align:left;">更新资源上的标签</td></tr> <tr><td style="text-align:left;">其他命令</td> <td style="text-align:left;">cluster-info</td> <td style="text-align:left;">集群信息</td> <td style="text-align:left;">显示集群信息</td></tr> <tr><td style="text-align:left;"></td> <td style="text-align:left;">version</td> <td style="text-align:left;">版本</td> <td style="text-align:left;">显示当前Server和Client的版本</td></tr></tbody></table> <p>下面以一个namespace / pod的创建和删除简单演示下命令的使用：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 创建一个namespace</span>
kubectl create namespace dev

namespace/dev created

<span class="token comment"># 获取namespace</span>
kubectl get ns

NAME              STATUS   AGE
default           Active   5h1m
dev               Active   15s
kube-node-lease   Active   5h1m
kube-public       Active   5h1m
kube-system       Active   5h1m

<span class="token comment"># 在此namespace下创建并运行一个nginx的Pod</span>
kubectl run pod --image<span class="token operator">=</span>nginx:latest -n dev

pod/pod created

<span class="token comment"># 查看新创建的pod</span>
kubectl get pod -n dev

NAME  READY   STATUS    RESTARTS   AGE
pod   <span class="token number">1</span>/1     Running   <span class="token number">0</span>          21s

<span class="token comment"># 删除指定的pod</span>
kubectl delete pod pod -n dev

pod <span class="token string">&quot;pod&quot;</span> deleted

<span class="token comment"># 删除指定的namespace</span>
kubectl delete ns dev

namespace <span class="token string">&quot;dev&quot;</span> deleted
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="_3-3-2-命令式对象配置"><a href="#_3-3-2-命令式对象配置" class="header-anchor">#</a> 3.3.2 命令式对象配置</h3> <p>命令式对象配置就是使用命令配合配置文件一起来操作kubernetes资源。</p> <p>1） 创建一个nginx-pod.yaml，内容如下：</p> <div class="language-yaml line-numbers-mode"><pre class="language-yaml"><code><span class="token punctuation">---</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Namespace
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> dev
<span class="token punctuation">---</span>
<span class="token key atrule">apiVersion</span><span class="token punctuation">:</span> v1
<span class="token key atrule">kind</span><span class="token punctuation">:</span> Pod
<span class="token key atrule">metadata</span><span class="token punctuation">:</span>
  <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>pod
  <span class="token key atrule">namespace</span><span class="token punctuation">:</span> dev
<span class="token key atrule">spec</span><span class="token punctuation">:</span>
  <span class="token key atrule">containers</span><span class="token punctuation">:</span>
  <span class="token punctuation">-</span> <span class="token key atrule">name</span><span class="token punctuation">:</span> nginx<span class="token punctuation">-</span>containers
    <span class="token key atrule">image</span><span class="token punctuation">:</span> nginx<span class="token punctuation">:</span>latest
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><p>2）执行create命令，创建资源：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl create -f nginx-pod.yaml

namespace/dev created
pod/nginx-pod created
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>此时发现创建了两个资源对象，分别是namespace和pod</p> <p>3）执行get命令，查看资源：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl get -f nginx-pod.yaml

NAME            STATUS   AGE
namespace/dev   Active   18s

NAME            READY   STATUS    RESTARTS   AGE
pod/nginx-pod    <span class="token number">1</span>/1     Running   <span class="token number">0</span>          17s
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>这样就显示了两个资源对象的信息</p> <p>4）执行delete命令，删除资源：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code>kubectl delete -f nginx-pod.yaml

namespace <span class="token string">&quot;dev&quot;</span> deleted
pod <span class="token string">&quot;nginx-pod&quot;</span> deleted
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>此时发现两个资源对象被删除了</p> <blockquote><p>总结: 命令式对象配置的方式操作资源，可以简单的认为：命令  +  yaml配置文件（里面是命令需要的各种参数）</p></blockquote> <h3 id="_3-3-3-声明式对象配置"><a href="#_3-3-3-声明式对象配置" class="header-anchor">#</a> 3.3.3 声明式对象配置</h3> <p>声明式对象配置跟命令式对象配置很相似，但是它只有一个命令apply。</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token comment"># 首先执行一次kubectl apply -f yaml文件，发现创建了资源</span>
kubectl apply -f nginx-pod.yaml

namespace/dev created
pod/nginx-pod created

<span class="token comment"># 再次执行一次kubectl apply -f yaml文件，发现说资源没有变动</span>
kubectl apply -f nginx-pod.yaml

namespace/dev unchanged
pod/nginx-pod unchanged
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><blockquote><p>总结:
其实声明式对象配置就是使用apply描述一个资源最终的状态（在yaml中定义状态）
使用apply操作资源：
如果资源不存在，就创建，相当于 <code>kubectl create</code>
如果资源已存在，就更新，相当于 <code>kubectl patch</code></p></blockquote> <h2 id="扩展-kubectl可以在node节点上运行吗"><a href="#扩展-kubectl可以在node节点上运行吗" class="header-anchor">#</a> 扩展：kubectl可以在node节点上运行吗 ?</h2> <p>kubectl的运行是需要进行配置的，它的配置文件是<code>$HOME/.kube</code>，如果想要在node节点运行此命令，需要将master上的.kube文件复制到node节点上，即在master节点上执行下面操作：</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">scp</span>  -r  <span class="token environment constant">HOME</span>/.kube   node1: <span class="token environment constant">HOME</span>/
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>使用推荐: 三种方式应该怎么用 ?</p> <blockquote><p>创建/更新资源 使用声明式对象配置 kubectl apply -f XXX.yaml</p> <p>删除资源 使用命令式对象配置 kubectl delete -f XXX.yaml</p> <p>查询资源 使用命令式对象管理 kubectl get(describe) 资源名称</p></blockquote></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">1/28/2022, 8:09:41 AM</span></div></footer> <!----> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-1-资源管理介绍" class="sidebar-link reco-side-_3-1-资源管理介绍" data-v-70334359>3.1 资源管理介绍</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-2-yaml语言介绍" class="sidebar-link reco-side-_3-2-yaml语言介绍" data-v-70334359>3.2 YAML语言介绍</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-3-资源管理方式" class="sidebar-link reco-side-_3-3-资源管理方式" data-v-70334359>3.3 资源管理方式</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-3-1-命令式对象管理" class="sidebar-link reco-side-_3-3-1-命令式对象管理" data-v-70334359>3.3.1 命令式对象管理</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-3-2-命令式对象配置" class="sidebar-link reco-side-_3-3-2-命令式对象配置" data-v-70334359>3.3.2 命令式对象配置</a></li><li class="level-3" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#_3-3-3-声明式对象配置" class="sidebar-link reco-side-_3-3-3-声明式对象配置" data-v-70334359>3.3.3 声明式对象配置</a></li><li class="level-2" data-v-70334359><a href="/blogs/k8s/k8s-resources-manager.html#扩展-kubectl可以在node节点上运行吗" class="sidebar-link reco-side-扩展-kubectl可以在node节点上运行吗" data-v-70334359>扩展：kubectl可以在node节点上运行吗 ?</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><div class="reco-bgm-panel" data-v-b1d3339e><audio id="bgm" src="/music/1.m4a" data-v-b1d3339e></audio> <div class="reco-float-box" style="bottom:200px;z-index:999999;display:none;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><img src="https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg" data-v-b1d3339e></div> <div class="reco-bgm-box" style="left:10px;bottom:10px;z-index:999999;" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="reco-bgm-cover" style="background-image:url(https://y.qq.com/music/photo_new/T002R300x300M0000046Etze42qCxC_1.jpg);" data-v-b1d3339e><div class="mini-operation" style="display:none;" data-v-b1d3339e><i class="reco-bgm reco-bgm-pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play" style="display:none;" data-v-b1d3339e></i></div> <div class="falut-message" style="display:none;" data-v-b1d3339e>
          播放失败
        </div></div> <div class="reco-bgm-info" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-music music" data-v-b1d3339e></i>苍穹</div> <div class="info-box" data-v-b1d3339e><i class="reco-bgm reco-bgm-artist" data-v-b1d3339e></i>韩磊</div> <div class="reco-bgm-progress" data-v-b1d3339e><div class="progress-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div> <div class="reco-bgm-operation" data-v-b1d3339e><i class="reco-bgm reco-bgm-last last" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-pause pause" style="display:none;" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-play play" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-next next" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-volume1 volume" data-v-b1d3339e></i> <i class="reco-bgm reco-bgm-mute mute" style="display:none;" data-v-b1d3339e></i> <div class="volume-bar" data-v-b1d3339e><div class="bar" data-v-b1d3339e></div></div></div></div> <div class="reco-bgm-left-box" data-v-b1d3339e data-v-41bcba48 data-v-b1d3339e><i class="reco-bgm reco-bgm-left" data-v-b1d3339e></i></div></div></div><!----></div></div>
    <script src="/assets/js/app.b9ccfc55.js" defer></script><script src="/assets/js/4.8390fbcb.js" defer></script><script src="/assets/js/1.3334f995.js" defer></script><script src="/assets/js/31.fa9bd0f8.js" defer></script>
  </body>
</html>
